﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using Microsoft.WindowsAzure.MobileServices;
using System.Threading;

namespace jbgswin
{
    public partial class downld : PhoneApplicationPage
    {
        private BackgroundWorker backgroundworker;
        private IMobileServiceTable<dituwz> todoTable = App.MobileService.GetTable<dituwz>();
       // private MobileServiceCollection<dituwz, dituwz> bc;
        private List<dituwz> bca = new List<dituwz>();
        private static Semaphore sp = new Semaphore(1, 3);
        public downld()
        {
            InitializeComponent();
            this.ncct.Text = App.myditu.Count.ToString() + "条";
            this.cla1.ItemsSource = App.myditu.ToList();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bca = new List<dituwz>();
            // bca.Clear();
            ProgressBar1.Visibility = Visibility.Visible;

            ProgressBar1.IsIndeterminate = false;
            backgroundworker = new BackgroundWorker();//创建一个后台处理类的对象
            //调用RunWorkerAsync后台操作时引发此事件，即后台要处理的事情写在这个事件里面
            backgroundworker.DoWork += backgroundworker_DoWork;
            //当后台操作完成事件
            backgroundworker.RunWorkerCompleted += backgroundworker_RunWorkerCompleted;
            //当处理进度（ReportProgress）被激活后，进度的改变将触发ProgressChanged事件
            backgroundworker.ProgressChanged += backgroundworker_ProgressChanged;
            //设置为可报告进度情况的后台处理
            backgroundworker.WorkerReportsProgress = true;
            backgroundworker.RunWorkerAsync();
        }
        void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //把进度改变的值赋给ProgressBar1进度条的值
                ProgressBar1.Value = e.ProgressPercentage;
            });
        }
        void backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //隐藏进度条；
                ProgressBar1.Visibility = Visibility.Collapsed;
                fwqct.Text = bca.Distinct().Count().ToString() + "条";
                this.cla1.ItemsSource = bca.Distinct().OrderBy(h => h.nowid).ToList();
            });
        }

        private async void downtb()
        {
            try
            {
               
                for (int i = 0; i < 2; i++)
                {
                    var g = await todoTable.Skip(i * 50).Take(50).ToCollectionAsync();
                    // int a = 0;
                    List<dituwz> w = g.ToList();
                    foreach (dituwz f in w)
                    {
                        bca.Add(f);
                    }
                }
                sp.Release();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
        {
            // 
             downtb();
            for (int j = 0; j < 2; j++)
            {
                sp.WaitOne();
                for (int i = 0; i < 50; i++)
                {
                    i += 10;
                    //赋值当前进度的值，
                    backgroundworker.ReportProgress((j*50)+i+1);
                    //为了能看到进度条的效果。这里用进程暂停了1秒
                    System.Threading.Thread.Sleep(1000);
                    if (bca != null)
                    {
                        if (bca.Count > 50)
                        {
                            return;
                        }
                    }
                }
            }

            /*  for (int i = 20; i < 100; i++)
                    {
                      //  i += 1;
                        //赋值当前进度的值，
                        backgroundworker.ReportProgress(20+i);
                        //为了能看到进度条的效果。这里用进程暂停了1秒
                        System.Threading.Thread.Sleep(1000);
                        if (bc != null)
                        {
                            if (bca.Count > 50)
                            {
                                return;
                            }
                        }
                    }*/
            //  backgroundworker.ReportProgress(30);
            /* for (int i = 0; i < 2; i++)
             {*/

            /* var g = await todoTable.Skip(0 * 50).Take(50).ToCollectionAsync();
             // int a = 0;
             List<dituwz> w = g.ToList();
             /*foreach (dituwz f in w)
             {
                 bca.Add(f);
             }#1#
             for (int j = 0; j < 50; j++)
             {
                 bca.Add(w[j]);
                 backgroundworker.ReportProgress(j);
                 System.Threading.Thread.Sleep(1000);
             }
             //  MessageBox.Show((60 + (i*30)).ToString);
               */
            //   }
            //  backgroundworker.ReportProgress(90);
            //  return;

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            App.myditu.Clear();
            App.myditu = null;
            App.myditu = bca.Distinct().ToList();
            this.ncct.Text = App.myditu.Count.ToString()+"条";
        }
    }
}